package com.home.cop.employee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.home.cop.employee.mapper.SysRoleMapper;
import com.home.cop.employee.model.SysMenu;
import com.home.cop.employee.model.SysRole;
import com.home.cop.employee.model.SysRolePriv;
import com.home.cop.employee.service.SysRolePrivService;
import com.home.cop.employee.service.SysRoleService;
import com.home.cop.employee.service.SysUserService;
import com.home.cop.sysinfo.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRolePrivService rolePrivService;

    @Autowired
    private SysMenuService menuService;

    @Override
    public PageInfo<SysRole> list(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(roleMapper.list(params));
    }

    @Transactional
    @Override
    public void deleteRole(Integer roleId) {
        rolePrivService.deleteRoleMenu(roleId);
        roleMapper.deleteByPrimaryKey(roleId);
    }

    @Transactional
    @Override
    public void insert(SysRole role) {
        roleMapper.insert(role);
    }

    @Transactional
    @Override
    public void updateRoleInfo(SysRole role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    @Transactional
    @Override
    public void updateRoleStatus(Integer roleId, Integer status) {
        roleMapper.updateRoleStatus(roleId,status);
    }

    @Override
    public Map<String, Integer> countRoleUse(Integer roleId) {
        Map<String,Integer> maps = new HashMap<>();
        Integer userNum = userService.countUserByRoleId(roleId);
        Integer menuNum = rolePrivService.countRoleMenus(roleId);
        maps.put("userNum",userNum);
        maps.put("menuNum",menuNum);
        maps.put("employeeNum",0);
        return maps;
    }

    @Transactional
    @Override
    public void updateRoleMenus(Integer roleId, String menus) {
        rolePrivService.deleteRoleMenu(roleId);
        if (StringUtils.isBlank(menus)){
            return;
        }
        String[] menuArry = menus.split(",");
        for (String menuId:menuArry){
            SysRolePriv rolePriv = new SysRolePriv();
            rolePriv.setSysMenuId(Integer.valueOf(menuId));
            rolePriv.setSysRoleId(roleId);
            rolePrivService.insert(rolePriv);
        }
    }

    @Override
    public List<SysMenu> getRoleMenus(Integer roleId) {
        List<SysMenu> menus = menuService.getAllSysMenu();
        List<Integer> checkedMenus = rolePrivService.getRoleMenuIds(roleId);
        for (Integer menuId:checkedMenus){
            for (SysMenu menu:menus){
                if (menuId == menu.getSysMenuId()){
                    menu.setChecked(true);
                    break;
                }
            }
        }
        List<SysMenu> menuTree = menuService.menuToTree(menus);
        return menuTree;
    }

    @Override
    public List<SysRole> getAllRoles() {
        return null;
    }

}
